﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Core.Support;
using MS.InformationManagement.DAL.DocumentCenter;
using Microsoft.SharePoint;

namespace MS.InformationManagement.BLL.GestionDeUsuarios
{
    public class RemoveUserRolePermissionsCommand : IBaseCommand
    {

        #region fields

        string _targetUserLogin;
        string _targetIdPerfil;
        Stack<IBaseCommand> _commands;

        #endregion

        #region Constructors

        public RemoveUserRolePermissionsCommand(string userLogin, string idPerfil)
        {
            _targetUserLogin = userLogin;
            _targetIdPerfil = idPerfil;
        }

        #endregion

        #region Implemented Methods

        public void Execute()
        {
            _commands = new Stack<IBaseCommand>();
            using (var dc = new DCDocumentCenterDataContext(SPContext.Current.Web.Url))
            {
                var currentPerfil = (from perfil in dc.Perfiles
                                     where perfil.Perfil == _targetIdPerfil
                                     select perfil).FirstOrDefault();
                if (currentPerfil != null)
                    foreach (var role in currentPerfil.TipoRole)
                    {
                        if (!string.IsNullOrEmpty(role.Grupo))
                        {
                            RemoveUserPermissionsCommand removeCommand = new RemoveUserPermissionsCommand(_targetUserLogin, role);
                            removeCommand.Execute();
                            _commands.Push(removeCommand);
                        }
                    }
            }
        }

        public void Undo()
        {
            foreach (var command in _commands)
                command.Undo();
        }

        #endregion
    }
}
